from django.http import HttpResponse

def test_view(request):
    return HttpResponse("Hello from authapp!")


from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from django.contrib.auth import authenticate, login
from rest_framework_simplejwt.tokens import RefreshToken
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login

class LoginView(APIView):
    def post(self, request):
        username = request.data.get("username")
        password = request.data.get("password")
        user = authenticate(request, username=username, password=password)

        if user is not None:
            # 生成 JWT Token
            refresh = RefreshToken.for_user(user)
            return Response({
                'access': str(refresh.access_token),
                'refresh': str(refresh),
                'username': user.username
            })
        else:
            return Response({'error': '用户名或密码错误'}, status=status.HTTP_401_UNAUTHORIZED)


def login_page(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(username=username, password=password)

        if user:
            login(request, user)
            if user.role == 'advertiser':
                return redirect('advertiser_dashboard')
            elif user.role == 'editor':
                return redirect('editor_dashboard')
            elif user.role == 'manager':
                return redirect('review_dashboard')
            else:
                return redirect('/')  # fallback
        else:
            return render(request, 'login.html', {'error': '用户名或密码错误'})

    return render(request, 'login.html')

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import CustomUser

class RegisterView(APIView):
    def post(self, request):
        username = request.data.get("username")
        password = request.data.get("password")
        role = request.data.get("role")

        if not username or not password or not role:
            return Response({"error": "所有字段都是必须的"}, status=status.HTTP_400_BAD_REQUEST)

        if role not in ['advertiser', 'editor', 'manager']:
            return Response({"error": "角色不合法"}, status=status.HTTP_400_BAD_REQUEST)

        if CustomUser.objects.filter(username=username).exists():
            return Response({"error": "用户名已存在"}, status=status.HTTP_400_BAD_REQUEST)

        user = CustomUser.objects.create_user(username=username, password=password, role=role)
        user.save()

        return Response({"message": "注册成功"}, status=status.HTTP_201_CREATED)
from django.shortcuts import render

def register_page(request):
    if request.method == 'POST':
        username = request.POST.get("username")
        password = request.POST.get("password")
        role = request.POST.get("role")

        if not username or not password or not role:
            return render(request, 'register.html', {"error": "请填写所有字段"})

        if CustomUser.objects.filter(username=username).exists():
            return render(request, 'register.html', {"error": "用户名已存在"})

        CustomUser.objects.create_user(username=username, password=password, role=role)
        return render(request, 'register.html', {"success": "注册成功！"})

    return render(request, 'register.html')
